const Redis = require("../../../sql/redis");
const MYSQL = require("../../sql/mysql");
/**
 * 频道列表
 * @return {promise<{code:string,msg:string}>}
 */
async function GetChannelList(Request) {
    try {
        let Query = Request.query;
        let { page = 1, limit = 10 } = Query;
        limit = limit < 1 ? 10 : limit;
        page = page < 1 ? 1 : page;
        let SqlName = "";
        const Conline = await MYSQL.promise().getConnection();
        let [list] = await Conline.query(`
        SELECT * FROM ${SqlName}
        LIMIT ${(page - 1) * limit + "," + page * limit}
      `);
        // 查询完毕，释放池连接回池
        await Conline.release();
        return {
            code: 0,
            msg: "操作成功",
            data: {
                page,
                limit,
                list,
            },
        };
    } catch (error) {
        throw {
            code: 500,
            msg: "获取失败",
        };
    }
}
/**
 * 修改频道
 * @return {promise<{code:string,msg:string}>}
 */
async function UpdateChannel(Request) { }
/**
 * 创建频道
 * @return {promise<{code:string,msg:string}>}
 */
async function CreateChannel(Request) {
    try {
        let Body = Request.body;
        let { name, description } = Body;
        // 获取Mysql链接
        const Conline = await MYSQL.promise().getConnection();
        let SqlName = "";
        // 插入数据
        let [InsertResult] = await Conline.query(
            `INSERT INTO ${SqlName} SET?`,
            {
                name,
                description,
            }
        );
        // 查询完毕，释放池连接回池
        await Conline.release();
        let { affectedRows } = InsertResult;
        if (affectedRows) {
            return {
                code: 0,
                msg: "发布成功",
            };
        } else {
            throw { code: 500, msg: "发布失败,记录不存在" };
        }
    } catch (error) {
        let { msg } = error;
        if (msg) {
            throw error;
        } else {
            throw { code: 500, msg: "操作失败" };
        }
    }
}
/**
 * 删除频道
 * @return {promise<{code:string,msg:string}>}
 */
async function DeleteChannel(Request) {
    try {
        let Body = Request.body;
        let { id } = Body;
        // 获取Mysql链接
        const Conline = await MYSQL.promise().getConnection();
        let SqlName = "";
        // 更新状态为0
        let [UpdateResult] = await Conline.query(
            `UPDATE ${SqlName} SET ? WHERE id = ${id}`,
            {
                state: 0,
            }
        );
        // 查询完毕，释放池连接回池
        await Conline.release();
        let { affectedRows } = UpdateResult;
        if (affectedRows) {
            return {
                code: 0,
                msg: "删除成功",
            };
        } else {
            throw { code: 500, msg: "删除失败,记录不存在" };
        }
    } catch (error) {
        let { msg } = error;
        if (msg) {
            throw error;
        } else {
            throw { code: 500, msg: "操作失败" };
        }
    }
}
/**
 * 加入频道
 * @return {promise<{code:string,msg:string}>}
 */
async function JoinChannel(Request) { }
/**
 * 退出频道
 * @return {promise<{code:string,msg:string}>}
 */
async function QuitChannel(Request) { }
/**
 * 我加入的频道列表
 * @return {promise<{code:string,msg:string}>}
 */
async function MyJoinChannelList(Request) { }
/**
 * 我创建的的频道列表
 * @return {promise<{code:string,msg:string}>}
 */
async function MyCreatedChannelList(Request) { }
/**
 * 频道帖子列表
 * @return {promise<{code:string,msg:string}>}
 */
async function GetChannelDynamicList(Request) { }

module.exports = {
    GetChannelList,
    UpdateChannel,
    CreateChannel,
    DeleteChannel,
    JoinChannel,
    QuitChannel,
    MyJoinChannelList,
    MyCreatedChannelList,
    GetChannelDynamicList,
};